package vn.orderonline.web.admin.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import vn.orderonline.com.core.business.menudrink.IMenuDrink;
import vn.orderonline.com.core.mvc.model.MenuDrinkDto;

@Controller
@RequestMapping(value = "/menudrink")
public class MenuDrinkController {

	private static final Logger LOGGER = Logger
			.getLogger(MenuDrinkController.class);

	@Autowired
	private IMenuDrink iMenuDrink;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getListMenuDrink(@RequestParam Integer restaurantId,
			@RequestParam String restaurantName, ModelMap model) {
		LOGGER.info("START GET ALL MENU DRINK!");
		MenuDrinkDto menuDrink = null;
		try {
			menuDrink = new MenuDrinkDto();
			menuDrink.setRestaurantId(restaurantId);
			menuDrink.setRestaurantName(restaurantName);
			model.addAttribute("menuDrink", menuDrink);
			model.addAttribute("list",
					iMenuDrink.getListMenuDrinkByResId(restaurantId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN GET LIST MENU DRINK!", e);
		}
		LOGGER.info("END GET ALL MENU DRINK!");
		return "/menudrink/menudrink";
	}

	@RequestMapping(value = "/adddrink", method = RequestMethod.POST)
	public String addDrink(@ModelAttribute MenuDrinkDto menuDrink,
			RedirectAttributes redirectAttris) {
		LOGGER.info("START ADD NEW MENU DRINK!");
		try {
			iMenuDrink.saveMenuDrink(menuDrink);
			redirectAttris.addAttribute("restaurantId",
					menuDrink.getRestaurantId());
			redirectAttris.addAttribute("restaurantName",
					menuDrink.getRestaurantName());
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN ADD NEW MENU DRINK!", e);
		}
		LOGGER.info("END ADD NEW MENU DRINK!");
		return "redirect:/menudrink/list";
	}

	@RequestMapping(value = "/isExistedMenuDrink", method = RequestMethod.GET)
	@ResponseBody
	public String checkExistedMenuDrink(@RequestParam Integer restaurantId,
			@RequestParam String menuDrinkName) {
		LOGGER.info("START CHECK EXISTED MENU DRINK NAME!");
		Boolean flag = false;
		try {
			flag = iMenuDrink.checkExistedMenuDrinkName(restaurantId,
					menuDrinkName);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CHECK EXISTED MENU DRINK NAME!", e);
		}
		LOGGER.info("END CHECK EXISTED MENU DRINK NAME!" + flag.toString());
		return flag.toString();
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String deleteMenuDrink(@RequestParam Integer restaurantId,
			@RequestParam Integer menuDrinkId) {
		LOGGER.info("START DELETE MENU DRINK!");
		Boolean flag = false;
		try {
			iMenuDrink.deleteMenuDrink(restaurantId, menuDrinkId);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN DELETE MENU DRINK!" + flag.toString(), e);
		}
		LOGGER.info("END DELETE MENU DRINK!");
		return flag.toString();
	}

	@RequestMapping(value = "/formupdate", method = RequestMethod.GET)
	public String formUpdate(@RequestParam Integer restaurantId,
			@RequestParam String restaurantName,
			@RequestParam Integer menuDrinkId,
			@RequestParam String menuDrinkName, ModelMap model) {
		LOGGER.info("START CREATE FORM UPDATE MENU DRINK!");
		MenuDrinkDto menuDrink = null;
		try {
			menuDrink = new MenuDrinkDto();
			menuDrink.setRestaurantId(restaurantId);
			menuDrink.setRestaurantName(restaurantName);
			menuDrink.setMenuDrinkId(menuDrinkId);
			menuDrink.setMenuDrinkName(menuDrinkName);
		 	model.addAttribute("menuDrink", menuDrink);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CREATE FROM UPDATE MENU DRINK", e);
		}
		LOGGER.info("END CREATE UPDATE FORM UPDATE MENU DRINK!");
		return "/menudrink/updatemenudrink";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateMenuDrink(@ModelAttribute MenuDrinkDto menuDrink,
			RedirectAttributes redirectAttris) {
		LOGGER.info("START UPDATE MENU DRINK!");
		try {
			iMenuDrink.updateMenuDrink(menuDrink.getMenuDrinkId(),
					menuDrink.getMenuDrinkName());
			redirectAttris.addAttribute("restaurantId",
					menuDrink.getRestaurantId());
			redirectAttris.addAttribute("restaurantName",
					menuDrink.getRestaurantName());
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN UPDATE MENU DRINK!", e);
		}
		LOGGER.info("END UPDATE MENU DRINK!");
		return "redirect:/menudrink/list";
	}
}
